<?php

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
print_r("……………………………… " . date('Y-m-d H:i:s') . " start running get_pos_more_info_history.php………………………………");
$rate_rec = WposFinanceRate::where('status',1)->orderBy('id','desc')->first();
$wfds = WposFinanceDetail::select('id','status','payModeId','cardType','amount','refundAmount')->get();
foreach($wfds as $w){
    //计算手续费
    if($rate_rec){
        $an_fee = 0;
        if($w->status == '2') {
            if ($w->payModeId == '1006') {
                if ($w->cardType == 1 && $rate_rec->in_debit_rate) {
                    $an_fee = getAmount($w->amount,$rate_rec->in_debit_rate,$rate_rec->in_debit_sin_top);
                } elseif ($w->cardType == 2 && $rate_rec->in_credit_rate) {
                    $an_fee = getAmount($w->amount,$rate_rec->in_credit_rate,$rate_rec->in_credit_sin_top);
                } elseif ($w->cardType == 0 && $rate_rec->out_rate) {// 境外卡
                    $an_fee = getAmount($w->amount,$rate_rec->out_rate,$rate_rec->out_sin_top);
                }
            } elseif ($w->payModeId == '1003') {
                if ($rate_rec->b2c_wechat_rate) {
                    $an_fee = getAmount($w->amount,$rate_rec->b2c_wechat_rate,$rate_rec->b2c_wechat_sin_top);
                }
            } elseif ($w->payModeId == '1004') {
                if ($rate_rec->b2c_alipay_rate) {
                    $an_fee = getAmount($w->amount,$rate_rec->b2c_alipay_rate,$rate_rec->b2c_alipay_sin_top);
                }
            }
        }elseif($w->status == '3'){
            if ($w->payModeId == '1006') {
                if ($w->cardType == 1 && $rate_rec->in_debit_rate && $rate_rec->in_debit_back == '1') {
                    $an_fee = getAmount($w->amount,$rate_rec->in_debit_rate,$rate_rec->in_debit_sin_top);
                } elseif ($w->cardType == 2 && $rate_rec->in_credit_rate && $rate_rec->in_credit_back == '1') {
                    $an_fee = getAmount($w->amount,$rate_rec->in_credit_rate,$rate_rec->in_credit_sin_top);
                } elseif ($w->cardType == 0 && $rate_rec->out_rate && $rate_rec->out_back == '1') {// 境外卡
                    $an_fee = getAmount($w->amount,$rate_rec->out_rate,$rate_rec->out_sin_top);
                }
            } elseif ($w->payModeId == '1003') {
                if ($rate_rec->b2c_wechat_rate && $rate_rec->b2c_wechat_back == '1') {
                    $an_fee = getAmount($w->amount,$rate_rec->b2c_wechat_rate,$rate_rec->b2c_wechat_sin_top);
                }
            } elseif ($w->payModeId == '1004') {
                if ($rate_rec->b2c_alipay_rate && $rate_rec->b2c_alipay_back == '1') {
                    $an_fee = getAmount($w->amount,$rate_rec->b2c_alipay_rate,$rate_rec->b2c_alipay_sin_top);
                }
            }
        }
        try {
            WposFinanceDetail::where('id', $w->id)->update(array('rate_id' => $rate_rec->id, 'anhui_fee' => $an_fee, 'anhui_income_amt' => ($w->amount - $w->refundAmount - $an_fee)));
        }catch(Exception $e){
            print_r("get_pos_more_info_history.php 错误：" . json_encode($e));
        }
    }
}
function getAmount($amount,$rate,$top){
    $temp5 = round(($amount / 100) * ($rate / 100000), 2) * 100;
    if ($top) {
        $an_fee = $temp5 > $top ? $top : $temp5;
    } else {
        $an_fee = $temp5;
    }
    return $an_fee;
}
print_r("……………………………… " . date('Y-m-d H:i:s') . " stop running get_pos_more_info_history.php………………………………");
?>